//
//  GHttpEnqine.m
//  guess
//
//  Created by fank on 15/11/3.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "GHttpEnqine.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "JokeDataModel.h"
#import "GuessDataModel.h"

@implementation GHttpEnqine
+(void)getTheJokeWithTheType:(int)JokeType withThePage:(NSNumber *)page withComplete:(void(^)(id data))complete  withfailure:(void(^)(id error))failure
{
    NSString *url =(JokeType == JokeText) ? JOKE_TEXT_API:JOKE_PIC_API;
    [BaiduAPISendRequest requestWithUrl:url withPage:page withComplete:^(id data) {
        NSLog(@"%@",data);
        if (data ==nil) {
            failure(@"网络错误，请稍后再试");
        }
        
        JokeDataModel *model =[JokeDataModel modelWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] ];
        NSLog(@"%@",model.showapi_res_code);
        if (![model.showapi_res_code isEqual:@0] ) {
            failure(model.showapi_res_error);
        }else{
            
            complete(model.showapi_res_body);
        }
        
    } withfailure:^(id error) {
        failure(@"网络连接不良,请稍后再试");
    }];
    
    
}

+(void)getTheGuessWithThePage:(NSNumber *)page withComplete:(void(^)(id data))complete  withfailure:(void(^)(id error))failure
{
    NSString *url = GUEST_API;
    [BaiduAPISendRequest requestWithUrl:url withPage:@1 withComplete:^(id data) {
        NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        GuessDataModel *model =[[GuessDataModel alloc]init];
        model =(GuessDataModel *)[MTLJSONAdapter modelOfClass:[GuessDataModel class]fromJSONDictionary:dict error:nil];
        NSString *str;
        
        
        NSLog(@"%@",model.answer);
        complete(model);
        
    } withfailure:^(id error) {
        failure(@"网络连接不良,请稍后再试");
    }];

}


+ (void)getTheGuessWithUrl:(NSString *)url
                dictinory:(NSDictionary *)dictionary
                  success:(void (^)(id data))success
                     fail:(void (^)(id error))fail
{
    AFHTTPRequestOperationManager *manager = [ABTools managerwithURL:url];
    [manager POST:url parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail && error != nil) {
            fail(error);
        }
    }];
}



+(void)getQiuBaiVideoWithUrl:(NSString *)url
                        page:(NSNumber *)page
                     success:(void(^)(id data))success
                        fail:(void(^)(id error))fail
{
    [BaiduAPISendRequest requestWithUrl:url page:page success:^(id data) {
        if (success) {
            NSString *str1 = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            
            str1 = [str1 componentsSeparatedByString:@"<!-- Main content Start -->"][1];
            str1 = [str1 componentsSeparatedByString:@"<div class=\"pagebar clearfix\">"][0];
            
            NSMutableArray *array = [NSMutableArray arrayWithArray:[str1 componentsSeparatedByString:@"<div class=\"author"]];
            [array removeObjectAtIndex:0];
            
            NSMutableArray *array1 = [NSMutableArray array];
            
            for (NSString *temp in array) {
                NSString *str111 = [temp componentsSeparatedByString:@"<div class=\"stats\">"][0];
                [array1 addObject:str111];
            }
            
            NSMutableArray *list = [NSMutableArray array];
            for (NSString *temp in array1) {
                NSMutableArray *array2 =[NSMutableArray arrayWithArray:[temp componentsSeparatedByString:@"</div>"]];
                [array2 removeLastObject];
                
                NSString *headImage = [[array2[0] componentsSeparatedByString:@"<img src=\""][1] componentsSeparatedByString:@".jpg"][0];
                headImage = [NSString stringWithFormat:@"%@.jpg",headImage];
                
                NSString *autherName = [[array2[0] componentsSeparatedByString:@"<h2>"][1] componentsSeparatedByString:@"</h2>"][0];
                
                NSString *title = [[[array2[1] componentsSeparatedByString:@"<div class=\"content\">"][1] componentsSeparatedByString:@"<!--"][0] stringByReplacingOccurrencesOfString:@"\n" withString:@""] ;
                
                NSRange range1 = [array2[2] rangeOfString:@"poster=\""];
                NSRange range2 = [array2[2] rangeOfString:@".jpg"];
                NSString *poster = [array2[2] substringWithRange:NSMakeRange(range1.location+range1.length, range2.location-range1.location-range1.length + range2.length)];
                
             
                
                
                NSRange range3 = [array2[2] rangeOfString:@"<source src=\""];
                NSRange range4 = [array2[2] rangeOfString:@".mp4"];
                NSString *videoUrl = [array2[2] substringWithRange:NSMakeRange(range3.location+range3.length, range4.location-range3.location-range3.length+range4.length)];
                
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                [dict setObject:headImage forKey:@"headImage"];
                [dict setObject:title forKey:@"title"];
                [dict setObject:poster forKey:@"poster"];
                [dict setObject:videoUrl forKey:@"videoUrl"];
                [dict setObject:autherName forKey:@"autherName"];
                [list addObject:dict];
            }
            success(list);
        }
    } fail:^(id error) {
        if (fail) {
            fail(error);
        }
    }];
}


+(void)getYouYiSiPicWithUrl:(NSString *)url
                       page:(NSNumber *)page
                    success:(void(^)(id data))success
                       fail:(void(^)(id error))fail
{
    [BaiduAPISendRequest requestWithUrl:url page:page success:^(id data) {
        NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",html);
        
        NSMutableArray *htmlArray = [NSMutableArray arrayWithArray:[html componentsSeparatedByString:@"<div class=\"mainlist\">"]];
        [htmlArray removeObjectAtIndex:0];
        NSString *string1 = [[htmlArray lastObject] componentsSeparatedByString:@"<div class=\"pageli\">"][1];
        NSString *string2 = [[htmlArray lastObject] componentsSeparatedByString:@"<div class=\"pageli\">"][0];
        [htmlArray removeLastObject];
        [htmlArray addObject:string2];
        
        NSMutableArray *infoArray = [NSMutableArray array];
        for (NSString *temp in htmlArray) {
            NSRange range1 = [temp rangeOfString:@"<a href=\""];
            NSRange range2 = [temp rangeOfString:@"\" target=\"_blank\"><img"];
            NSString *clickUrl = [temp substringWithRange:NSMakeRange(range1.location+range1.length, range2.location-(range1.location+range1.length))];
            
            NSRange range3 = [temp rangeOfString:@"<img src=\""];
            NSRange range4 = [temp rangeOfString:@"\" alt="];
            NSString *imageUrl = [temp substringWithRange:NSMakeRange(range3.location+range3.length, range4.location-(range3.location+range3.length))];
            
            NSRange range5 = [temp rangeOfString:@"["];
            NSRange range6 = [temp rangeOfString:@"]"];
            NSString *title = [temp substringWithRange:NSMakeRange(range5.location+range5.length, range6.location-(range5.location+range5.length))];
            
            NSRange range8 = [[temp componentsSeparatedByString:@"class=\"link4\" target=\"_blank\">"][1] rangeOfString:@"</a></div>"];
            NSString *autherName = [[temp componentsSeparatedByString:@"class=\"link4\" target=\"_blank\">"][1]  substringWithRange:NSMakeRange(0, range8.location) ];
            
            NSRange range9 = [temp rangeOfString:@"作者：<a href=\""];
            NSRange range10 = [temp rangeOfString:@"\" class=\"link4"];
            NSString *autherUrl = [temp substringWithRange:NSMakeRange(range9.location+range9.length, range10.location-(range9.location+range9.length))];
            
            NSRange range11 = [temp rangeOfString:@"推荐于："];
            NSString *date = [temp substringWithRange:NSMakeRange(range11.location+range11.length,10)];
            
            NSRange range12 = [temp rangeOfString:@"<div class=\"summary\">"];
            NSRange range13 = [temp rangeOfString:@"<div class=\"data\">"];
            NSString *summary = [temp substringWithRange:NSMakeRange(range12.location+range12.length, range13.location-(range12.location+range12.length))];
            summary = [summary componentsSeparatedByString:@"</div>"][0];
            
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObject:clickUrl forKey:@"clickUrl"];
            [dict setObject:imageUrl forKey:@"imageUrl"];
            [dict setObject:summary forKey:@"summary"];
            [dict setObject:title forKey:@"title"];
            [dict setObject:autherName forKey:@"autherName"];
            [dict setObject:autherUrl forKey:@"autherUrl"];
            [dict setObject:date forKey:@"date"];
            [infoArray addObject:dict];
        }
        NSString *str3 = [[string1 componentsSeparatedByString:@"<h2>热门标签</h2>"][1] componentsSeparatedByString:@"<h2>精彩评论</h2>"][0];
        NSArray *list11 = [str3 componentsSeparatedByString:@"<a href=\""];
        NSMutableArray *list22 = [NSMutableArray arrayWithArray:list11];
        [list22 removeObjectAtIndex:0];
        NSMutableArray *titleList = [NSMutableArray array];
        for (NSString *temp1 in list22) {
            NSString *string44 = [temp1 componentsSeparatedByString:@"\" target="][0];
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            NSRange range1 = [string44 rangeOfString:@"/tag/"];
            NSRange range2 = [string44 rangeOfString:[string44 substringFromIndex:string44.length-2]];
            NSString *string55 = [string44 substringWithRange:NSMakeRange(range1.location+range1.length, range2.location-(range1.location+range1.length))];
            [dict setObject:string44 forKey:string55];
            [titleList addObject: dict];
        }
        // http://www.u148.net
        NSMutableDictionary *returnDict = [NSMutableDictionary dictionary];
        [returnDict setObject:infoArray forKey:@"info"];
        [returnDict setObject:titleList forKey:@"titleList"];
        success(returnDict);
    } fail:^(id error) {
        if (fail) {
            fail(error);
        }
    }];
}

+(void)getCaoEggWithUrl:(NSString *)url
                       page:(NSNumber *)page
                    success:(void(^)(id data))success
                       fail:(void(^)(id error))fail
{
    [BaiduAPISendRequest requestWithUrl:url page:page success:^(id data) {
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        
        
    } fail:^(id error) {
        if (fail) {
            fail(error);
        }
    }];
}


@end
